package com.text3;

import java.sql.*;
import java.util.Date;


public class Cha {
    public static void main(String[] args) throws SQLException {
        Cha cha = new Cha();
        cha.selectById(6,7,"null","null","null","null","null","2022-03-31 08:54:12","2022-03-31 08:54:12");
    }
    public void selectById(int id, int categoryId , String title, String summary, String content, String picPath, String author, String createDate, String modifyDate ) throws SQLException {
        Connection connection=null;
        Statement statement=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password="123456";
            connection= DriverManager.getConnection(url,username,password);
            String sql="SELECT * FROM news_detail WHERE id=? and categoryId=? and title=? and summary=? and content=? and picPath =?and author=? and createDate=? and  modifyDate =?";
            //statement=connection.createStatement();//不带条件的查询
            //带条件的查询
            preparedStatement= connection.prepareStatement(sql);

            preparedStatement.setInt(1,id);
            preparedStatement.setInt(2,categoryId);
            preparedStatement.setString(3,title);
            preparedStatement.setString(4,summary);
            preparedStatement.setString(5,content);
            preparedStatement.setString(6,picPath);
            preparedStatement.setString(7,author);
            preparedStatement.setString(8,createDate);
            preparedStatement.setString(9,modifyDate);
            resultSet=preparedStatement.executeQuery();


            while (resultSet.next()){
                int ids=resultSet.getInt("id");
                int name1=resultSet.getInt("categoryId");
                String date1 = resultSet.getString("title");
                String date2 = resultSet.getString("summary");
                String date3 = resultSet.getString("content");
                String date4 = resultSet.getString("picPath");
                String date5 = resultSet.getString("author");
                String date6 = resultSet.getString("createDate");
                String date7 = resultSet.getString("modifyDate");
/*
                Date date6=resultSet.getDate("createDate");
                Date date7=resultSet.getDate("modifyDate");*/

                System.out.println(ids+"-----"+name1 + "------" + date1+ "------" + date2+ "------" + date3+ "------" + date4 + "------" + date5+ "------" + date6+ "------" + date7);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            resultSet.close();
            preparedStatement.close();
            connection.close();
        }
    }
}
